import http from '../utils/http';

interface ILoginReq {
  username: string;
  password: string;
}

interface ILoginRes {
  refresh: string;
  expire: Date;
  token: string;
  username: string;
  email: string;
  id: number;
  role: boolean;
  avatar: string;
}

interface IRegisterReq {
  username: string;
  password: string;
  code: string;
  email: string;
}

interface IRegisterRes {
  message: string;
}

interface IUserDetail {
  id: number;
  rating: number;
  background: string;
  // eslint-disable-next-line camelcase
  profile_picture: string;
  introduce: string;
  person: number;
  // eslint-disable-next-line camelcase
  last_clockin: Date;
  luck: number;
}

interface Userdetails {
  username?: string;
  // eslint-disable-next-line camelcase
  last_login?: Date;
  // eslint-disable-next-line no-undef
  detail?: IUserDetail;
}

export const refreshToken = async (data: { refresh: string }) => {
  return (await http.post('/person/token/refresh/', data)) as any;
};

export const getUserDetails = async () => {
  return (await http.get('/person/detail/')) as Userdetails;
};
export const login = async (data: ILoginReq) => {
  return await http.post('/person/login/', data) as Promise<ILoginRes>;
};

export const register = async (data: IRegisterReq) => {
  return (await http.post('/person/register/', data)) as Promise<IRegisterRes>;
};

export const sendCodeByEmail = async (data: { email: string }) => {
  return (await http.post('/person/sendCode/', data)) as Promise<{ message: string }>;
};

export const getUserList = async () => {
  return (await http.get('/person/list/')) as any;
};

export const deleteUser = async (email: string) => {
  return (await http.post('/user/delete', { email })) as any;
};

export const getProfile = async (username: string) => {
  return (await http.get('/person/detail/', { username })) as Userdetails;
};

export const getUserRank = async () => {
  return (await http.get('/person/rank/')) as any;
};

export const resetPassword = async (email: string) => {
  return (await http.post('/user/resetPassword', { email })) as any;
};

export const getTaskState = async (task: string) => {
  return (await http.post('/task/state/', { task_id: task })) as { result: string , info: object };
};


export const saveLuck = async (username: string, luck: number, last_clockin: Date) => {
  return (await http.post('/person/saveLuck/', {
    'username': username,
    'luck': luck,
    'last_clockin': last_clockin
  })) as any;
};
